package cn.dinkevin.xui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import cn.dinkevin.xui.R;
import cn.dinkevin.xui.finder.ViewFinder;
import cn.dinkevin.xui.util.StringUtil;

/**
 * 确认对话框
 * Created by chengpengfei on 2017/5/4.
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public class ConfirmDialog {





    /**
     * Builder 工具类
     */
    public static class Builder{

        private Context context;
        private boolean showCancel = true ,cancelable = true, touchOutCancelable = true;     // 是否展示取消，是否可以取消，是否点击外部自动取消
        private String title,content;           // 标题、内容
        private String cancelText, okText;     // 取消按钮文字显示、确定按钮的文字显示
        private int cancelTextColor, okTextColor;        // 取消按钮文字颜色、确定按纽的文字颜色

        public Builder(Context context){
            this.context = context;
        }

        public ConfirmDialog.Builder setTitle(String title){
            this.title = title;
            return this;
        }

        public ConfirmDialog.Builder setContent(String content){
            this.content = content;
            return this;
        }

        public ConfirmDialog.Builder setShowCancel(boolean showCancel){
            this.showCancel = showCancel;
            return this;
        }

        public ConfirmDialog.Builder setCancelable(boolean cancelable){
            this.cancelable = cancelable;
            return this;
        }

        public ConfirmDialog.Builder setTouchOutCancelable(boolean touchOutCancelable){
            this.touchOutCancelable = touchOutCancelable;
            return this;
        }

        public ConfirmDialog.Builder setCancelTextColor(int cancelTextColor){
            this.cancelTextColor = cancelTextColor;
            return this;
        }

        public ConfirmDialog.Builder setOkTextColor(int okTextColor){
            this.okTextColor = okTextColor;
            return this;
        }

        public ConfirmDialog.Builder setCancelText(String cancelText){
            this.cancelText = cancelText;
            return this;
        }

        public ConfirmDialog.Builder setOKText(String okText){
            this.okText = okText;
            return this;
        }

        private ConfirmDialogInterface.DialogResultListener resultListener;
        public ConfirmDialog.Builder setResultListener(ConfirmDialogInterface.DialogResultListener listener){
            this.resultListener = listener;
            return this;
        }

        private DialogInterface.OnDismissListener dismissListener;
        public ConfirmDialog.Builder setDismissListener(DialogInterface.OnDismissListener dismissListener){
            this.dismissListener = dismissListener;
            return this;
        }

        public Dialog show(){
            LayoutInflater inflater = LayoutInflater.from(context);
            View contentView = inflater.inflate(R.layout.xui_confirm_dialog, null);
            final Dialog confirmDialog = new Dialog(context,R.style.TransDialogStyle);    // 创建自定义样式dialog
            confirmDialog.setContentView(contentView);
            confirmDialog.setCancelable(cancelable);
            confirmDialog.setCanceledOnTouchOutside(touchOutCancelable);

            ViewFinder finder = new ViewFinder(contentView);
            TextView txt_title = finder.findViewById(R.id.txt_title);
            TextView txt_message = finder.findViewById(R.id.txt_content);
            TextView btn_ok = finder.findViewById(R.id.txt_ok);

            // 标题
            txt_title.setText(title);
            txt_title.setVisibility(StringUtil.isEmpty(title) ? View.GONE : View.VISIBLE);

            // 内容
            txt_message.setText(content);
            txt_message.setMovementMethod(ScrollingMovementMethod.getInstance());
            txt_message.setVisibility(StringUtil.isEmpty(content) ? View.GONE : View.VISIBLE);
            btn_ok.setOnClickListener(v -> {
                confirmDialog.cancel();
                confirmDialog.dismiss();
                if(resultListener != null)
                    resultListener.onConfirmResult(true);
            });

            if(okTextColor > 0)
                btn_ok.setTextColor(okTextColor);

            if(!StringUtil.isEmpty(okText))
                btn_ok.setText(okText);

            // 判断是否显示取消按钮
            TextView txt_cancel = finder.findViewById(R.id.txt_cancel);
            txt_cancel.setVisibility(showCancel ? View.VISIBLE : View.GONE);
            if(showCancel){
                txt_cancel.setOnClickListener(v -> {
                    confirmDialog.cancel();
                    if(resultListener != null)
                        resultListener.onConfirmResult(false);
                });

                if(cancelTextColor > 0)
                    txt_cancel.setTextColor(cancelTextColor);

                if(!StringUtil.isEmpty(cancelText))
                    txt_cancel.setText(cancelText);
            }

            Window window = confirmDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setGravity(Gravity.CENTER);
            window.setAttributes(lp);
            confirmDialog.setOnDismissListener(dismissListener);
            confirmDialog.show();
            return confirmDialog;
        }
    }

    public void close(Dialog dialog){
        if(null != dialog && dialog.isShowing()){
            dialog.cancel();
            dialog.dismiss();
        }
    }
}
